<!--
 * @Author: 涵崽
 * @Date: 2023-11-06 15:36:54
 * @email: 1045654@qq.com
 * @gitee: https://gitee.com/han-zai
 * @LastEditors: 涵崽
 * @LastEditTime: 2023-11-10 09:44:45
 * @Description: 登录页-canvas背景动画
-->
<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h1 class="title">登录</h1>
      </div>

      <el-form-item prop="username">
        <div class="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            class="input"
            ref="username"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
        </div>
      </el-form-item>

      <el-form-item prop="password">
        <div class="pwd">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            class="input"
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @keyup.enter.native="handleLogin"
          />

          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </div>
      </el-form-item>
      <div class="btn">
        <el-button
          :loading="loading"
          type="primary"
          style="width: 380px"
          @click.native.prevent="handleLogin"
          >立即登录</el-button
        >
      </div>
    </el-form>

    <canvas
      width="1158"
      height="923"
      style="
        display: block;
        position: absolute;
        top: 0px;
        left: 0px;
        height: 100%;
        width: 100%;
        overflow: hidden;
        pointer-events: none;
        z-index: -1;
        opacity: 0.8;
      "
    ></canvas>
    <div class="tootip">
      <span>请旋转您的手机</span>
      <img src="../../assets/tootip.jpeg" alt="" />
    </div>
  </div>
</template>
<script>
import CanvasNest from "canvas-nest.js";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length <= 0) {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码长度不能低于6位"));
      } else {
        callback();
      }
    };
    return {
      config: {
        color: "0,180,235",
        pointColor: "234,123,123",
        count: 210,
        opacity: 0.8,
      },
      loginForm: {
        username: "admin",
        password: "111111",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: null,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    new CanvasNest(document.body, this.config);
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery,
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login {
  width: 100vw;
  height: 100vh;
  background-size: 100% 100%;
  display: flex;

  .login-form {
    border-radius: 10px;
    width: 480px;
    height: 500px;
    position: absolute;
    // background-color: #fff;
    background: rgba(255, 255, 255, 0.1);
    // 背景的模糊大小通过下面的属性值大小来调制
    backdrop-filter: blur(30px);
    box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .title-container {
      width: 100%;
      margin-top: 50px;
      margin-bottom: 50px;
      .title {
        text-align: center;
      }
    }
    .username {
      width: 380px;
      margin: 0 auto;
      .svg-container {
        position: absolute;
        width: 30px;
        z-index: 9;
        top: 0;
        left: 60px;
      }
    }
    .pwd {
      width: 380px;
      margin: 0 auto;
      .svg-container {
        position: absolute;
        width: 30px;
        z-index: 9;
        top: 0;
        left: 60px;
      }
      .show-pwd {
        position: absolute;
        width: 30px;
        z-index: 9;
        top: 0;
        right: 60px;
        cursor: pointer;
      }
    }
    .btn {
      width: 100%;
      display: flex;
      justify-content: center;
      margin-top: 100px;
    }
  }
}
</style>
<style>
.el-input__inner {
  padding-left: 30px !important;
}
.el-form-item__error {
  margin-left: 50px;
}
</style>
